package vip.xiaonuo.modular.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.login.SysLoginUser;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.dbs.core.annotation.DataSource;
import vip.xiaonuo.modular.business.entity.UserComponentVo;
import vip.xiaonuo.modular.business.mapper.UserComponentMapper;
import vip.xiaonuo.modular.business.service.UserComponentService;
import vip.xiaonuo.modular.common.WdAnalysisConst;
import vip.xiaonuo.modular.manage.poidata.entity.SubTagTotal;
import vip.xiaonuo.modular.manage.poidata.mapper.BiPoiDataMapper;
import vip.xiaonuo.modular.manage.poidata.param.PoiDataParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: liu
 * @Date:2022/8/23
 */
@Service
public class UserComponentServiceImpl implements UserComponentService {

    @Resource
    private UserComponentMapper userComponentMapper;

    @Resource
    private BiPoiDataMapper biPoiDataMapper;

    @Override
    public Object list() {
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        Page<UserComponentVo> list = userComponentMapper.list(PageFactory.defaultPage(), sysLoginUser.getId());
        return new PageResult<>(list);
    }

    @Override
    public boolean updateEnable(UserComponentVo vo) {
        if (StringUtils.isEmpty(vo.getComponentId())
                || StringUtils.isEmpty(vo.getIsEnable())) {
            throw new ServiceException(9, "参数错误");
        }
        SysLoginUser sysLoginUser = LoginContextHolder.me().getSysLoginUser();
        LambdaQueryWrapper<UserComponentVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserComponentVo::getUserId,sysLoginUser.getId());
        wrapper.eq(UserComponentVo::getComponentId, vo.getComponentId());
        wrapper.last(" limit 1 ");
        UserComponentVo userComponentVo = userComponentMapper.selectOne(wrapper);
        if (userComponentVo == null) {
            vo.setUserId(sysLoginUser.getId());
            vo.setCreateUser(sysLoginUser.getId());
            vo.setCreateTime(new Date());
            userComponentMapper.insert(vo);
        }else{
            userComponentVo.setUpdateUser(sysLoginUser.getId());
            userComponentVo.setUpdateTime(new Date());
            userComponentVo.setIsEnable(vo.getIsEnable());
            userComponentMapper.updateById(userComponentVo);
        }
        return true;
    }

    /**
     * 商业网点poi查询(大类=购物消费 )
     * @param vo
     * @return
     */
    @DataSource(name=WdAnalysisConst.CLICKHOUSE_DATASOURCE)
    @Override
    public Object commercialPoiQuery(PoiDataParam vo) {
        if (vo.getYear() == null) {
            throw new ServiceException(9, "参数错误");
        }
        vo.setTag("购物消费");
        List<SubTagTotal> subTagTotals = new ArrayList<>();
        if(StringUtils.hasText(vo.getCityName())) {
            subTagTotals = biPoiDataMapper.groupByCountryName(vo);
        }else if(StringUtils.hasText(vo.getProvName())){
            subTagTotals = biPoiDataMapper.groupByCityName(vo);
        }else{
            subTagTotals = biPoiDataMapper.groupByProvName(vo);
        }
        return subTagTotals;
    }
}
